#pragma once
#include "DirectXFramework.h"

void CDirectXFramework::Init(HWND& hWnd, HINSTANCE& hInst, bool bWindowed)
{
	m_hWnd = hWnd;

	//////////////////////////////////////////////////////////////////////////
	// Direct3D Foundations - D3D Object, Present Parameters, and D3D Device
	//////////////////////////////////////////////////////////////////////////

	// Create the D3D Object
	m_pD3DObject = Direct3DCreate9(D3D_SDK_VERSION);

	// Find the width and height of window using hWnd and GetWindowRect()
	RECT rect;
	GetWindowRect(hWnd, &rect);
	int width = rect.right - rect.left;
	int height = rect.bottom - rect.top;

	// Set D3D Device presentation parameters before creating the device
	D3DPRESENT_PARAMETERS D3Dpp;
	ZeroMemory(&D3Dpp, sizeof(D3Dpp));  // NULL the structure's memory

	D3Dpp.hDeviceWindow					= hWnd;										// Handle to the focus window
	D3Dpp.Windowed						= bWindowed;								// Windowed or Full-screen boolean
	D3Dpp.AutoDepthStencilFormat		= D3DFMT_D24S8;								// Format of depth/stencil buffer, 24 bit depth, 8 bit stencil
	D3Dpp.EnableAutoDepthStencil		= TRUE;										// Enables Z-Buffer (Depth Buffer)
	D3Dpp.BackBufferCount				= 1;										// Change if need of > 1 is required at a later date
	D3Dpp.BackBufferFormat				= D3DFMT_X8R8G8B8;							// Back-buffer format, 8 bits for each pixel
	D3Dpp.BackBufferHeight				= height;									// Make sure resolution is supported, use adapter modes
	D3Dpp.BackBufferWidth				= width;									// (Same as above)
	D3Dpp.SwapEffect					= D3DSWAPEFFECT_DISCARD;					// Discard back-buffer, must stay discard to support multi-sample
	D3Dpp.PresentationInterval			= m_bVsync ? D3DPRESENT_INTERVAL_DEFAULT : D3DPRESENT_INTERVAL_IMMEDIATE; // Present back-buffer immediately, unless V-Sync is on								
	D3Dpp.Flags							= D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;		// This flag should improve performance, if not set to NULL.
	D3Dpp.FullScreen_RefreshRateInHz	= bWindowed ? 0 : D3DPRESENT_RATE_DEFAULT;	// Full-screen refresh rate, use adapter modes or default
	D3Dpp.MultiSampleQuality			= 0;										// MSAA currently off, check documentation for support.
	D3Dpp.MultiSampleType				= D3DMULTISAMPLE_NONE;						// MSAA currently off, check documentation for support.

	// Check device capabilities
	DWORD deviceBehaviorFlags = 0;
	m_pD3DObject->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_D3DCaps);

	// Determine vertex processing mode
	if(m_D3DCaps.DevCaps & D3DCREATE_HARDWARE_VERTEXPROCESSING)
	{
		// Hardware vertex processing supported? (Video Card)
		deviceBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;	
	}
	else
	{
		// If not, use software (CPU)
		deviceBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING; 
	}
	
	// If hardware vertex processing is on, check pure device support
	if(m_D3DCaps.DevCaps & D3DDEVCAPS_PUREDEVICE && deviceBehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING)
	{
		deviceBehaviorFlags |= D3DCREATE_PUREDEVICE;	
	}
	
	// Create the D3D Device with the present parameters and device flags above
	m_pD3DObject->CreateDevice(
		D3DADAPTER_DEFAULT,		// which adapter to use, set to primary
		D3DDEVTYPE_HAL,			// device type to use, set to hardware rasterization
		hWnd,					// handle to the focus window
		deviceBehaviorFlags,	// behavior flags
		&D3Dpp,					// presentation parameters
		&m_pD3DDevice);			// returned device pointer

	//////////////////////////////////////////////////////////////////////////
	// Create a Font Object
	//////////////////////////////////////////////////////////////////////////

	// Load D3DXFont, each font style you want to support will need an ID3DXFont
	D3DXCreateFont(m_pD3DDevice, 30, 0, FW_BOLD, 0, false, DEFAULT_CHARSET, 
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arail"), &m_pD3DFont);

	//////////////////////////////////////////////////////////////////////////
	// Create Sprite Object and Textures
	//////////////////////////////////////////////////////////////////////////

	// Create a sprite object, note you will only need one for all 2D sprites
	D3DXCreateSprite(m_pD3DDevice, &m_pD3DSprite);

	// Load sprite textures
	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card_back.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[0], 0, &m_pTexture[0]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card1.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[1], 0, &m_pTexture[1]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card2.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[2], 0, &m_pTexture[2]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card3.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[3], 0, &m_pTexture[3]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card4.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[4], 0, &m_pTexture[4]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card5.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[5], 0, &m_pTexture[5]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card6.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[6], 0, &m_pTexture[6]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card7.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[7], 0, &m_pTexture[7]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card8.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[8], 0, &m_pTexture[8]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card9.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[9], 0, &m_pTexture[9]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"card10.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[10], 0, &m_pTexture[10]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"cardJ.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[11], 0, &m_pTexture[11]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"cardQ.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[12], 0, &m_pTexture[12]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"cardK.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[13], 0, &m_pTexture[13]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"cardA.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &m_imageInfo[14], 0, &m_pTexture[14]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"diamonds.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &suitimageInfo[0], 0, &suitTexture[0]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"clubs.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &suitimageInfo[1], 0, &suitTexture[1]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"hearts.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &suitimageInfo[2], 0, &suitTexture[2]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"spades.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &suitimageInfo[3], 0, &suitTexture[3]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"error.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &suitimageInfo[4], 0, &suitTexture[4]);

	D3DXCreateTextureFromFileEx(m_pD3DDevice, L"Title.png", 0, 0, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), &imageInfo, 0, &texture);

	// Seed rand() with time
	srand(timeGetTime());
	menuState = Main;
	// Initialize each card
	deck.rotation = D3DXToRadian(360);
	deck.scale = 0.7f;
	deck.color = D3DCOLOR_ARGB(255, 255, 255, 255);
	deck.value = 0;
	deck.faceup = false;

	drawpile.Reset();

	/*for(int i = 0; i < 5; i++)
	{
		com.SetCard(i, (, true, D3DXToRadian(360), 0.7f, D3DCOLOR_ARGB(255, 255, 255, 255));
	}

	for(int i = 0; i < 5; i++)
	{
		hmn.SetCard(i, (2 + rand() % 13), true, D3DXToRadian(360), 0.7f, D3DCOLOR_ARGB(255, 255, 255, 255));
	}*/

	win = 0;
	loss = 0;
	hide = true;
	winner = 0;
	menuState = Main;
	step = Deal;
	timepassed = 0;
	flipspeed = 0.1f;
	movespeed = 30.0f;

	deck.position = D3DXVECTOR3(64.0f, 88.0f, 0.0f);

	comstart[0] = D3DXVECTOR3(288.0f, 88.0f, 0.0f);
	comstart[1] = D3DXVECTOR3(400.0f, 88.0f, 0.0f);
	comstart[2] = D3DXVECTOR3(512.0f, 88.0f, 0.0f);
	comstart[3] = D3DXVECTOR3(624.0f, 88.0f, 0.0f);
	comstart[4] = D3DXVECTOR3(736.0f, 88.0f, 0.0f);
	
	hmnstart[0] = D3DXVECTOR3(288.0f, 424.0f, 0.0f);
	hmnstart[1] = D3DXVECTOR3(400.0f, 424.0f, 0.0f);
	hmnstart[2] = D3DXVECTOR3(512.0f, 424.0f, 0.0f);
	hmnstart[3] = D3DXVECTOR3(624.0f, 424.0f, 0.0f);
	hmnstart[4] = D3DXVECTOR3(736.0f, 424.0f, 0.0f);

	// Get starting positions
	for(int i=0; i < 5; i++)
	{
		com.SetCardPosition(i, D3DXVECTOR3(64.0f, 88.0f, 0.0f));
		hmn.SetCardPosition(i, D3DXVECTOR3(64.0f, 88.0f, 0.0f));
	}

	DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_pDIObject, NULL);
	
	//Create Device for Keyboard and Mouse
	m_pDIObject->CreateDevice(GUID_SysKeyboard, &m_pDIKeyboard, NULL); 
	m_pDIObject->CreateDevice(GUID_SysMouse, &m_pDIMouse, NULL);

	//Set Data Format for Keyboard and Mouse
	m_pDIKeyboard->SetDataFormat(&c_dfDIKeyboard); 
	m_pDIMouse->SetDataFormat(&c_dfDIMouse2);

	//Set the Cooperative Level
	m_pDIKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
	m_pDIMouse->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 

	//Acquire the Device 
	hr = m_pDIKeyboard->Acquire();
	hr = m_pDIMouse->Acquire();
}